﻿/**********************************************************\
*  TinyLib - Sprite                                        *
*                                                          *
*  Non-animated sprites.                                   *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AALib.Sprite
{
    public class Sprite : Node
    {
        public float m_rotation = 0.0f;
        public Color m_color = Color.White;

        protected Texture2D m_texture = null;

        public float m_layerDepth = 0.0f;
        public SpriteEffects m_effect = SpriteEffects.None;

        protected Rectangle m_sourceRectangle;

        public Texture2D Texture
        {
            get { return m_texture; }
        }

        public Rectangle SourceRectangle
        {
            get { return m_sourceRectangle; }
            set
            {
                m_sourceRectangle = value;

                if (value.X < 0) { m_sourceRectangle.X = 0; }
                if (value.Y < 0) { m_sourceRectangle.Y = 0; }
                if (value.Width > m_texture.Width) { m_sourceRectangle.Width = m_texture.Width; }
                if (value.Height > m_texture.Height) { m_sourceRectangle.Height = m_texture.Height; }
            }
        }

        public Sprite(SpriteBatch p_spriteBatch, Texture2D p_texture, Game p_game)
            : base(p_spriteBatch, p_game)
        {
            this.m_texture = p_texture;
            if (this.m_texture != null)
            {
                m_sourceRectangle.X = 0;
                m_sourceRectangle.Y = 0;
                m_sourceRectangle.Width = p_texture.Width;
                m_sourceRectangle.Height = p_texture.Height;
            }
        }

        public override void Draw(GameTime p_gameTime)
        {
            if (Visible)
            {
                m_spritebatch.Draw(m_texture, m_position, m_sourceRectangle, m_color, m_rotation, m_origin, m_scale, m_effect, m_layerDepth);
            }
        }
    }
}
